<?php

/**
 * 栏目相关接口
 */
class a_class extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->CI = & get_instance();
    }
    /**
     * 获取一级栏目
     * @param int $configid
     * @param int $classid
     * @return array
     */
    function get_child_class($configid = 1, $classid = 0) {
        $where = array(
            'configid' => $configid,
            'parentid' => $classid,
            'isclose' => 0,
        );
        $data = array(
            'from' => 'class',
            'column' => 'classlinkname,childnum,classid,isgotofirst,linkurl,classname,parentid',
            'where' => $where,
            'order' => 'listid desc',
        );
        return $this->m_public->get_list($data);
    }

    /**
     * 获取所有的子栏目
     * @param int $configid
     * @param int$classid
     * @return array
     */
    function get_child_class_all($configid = 1, $classid = 0) {
        $where = array(
            'configid' => $configid,
            'parentid' => $classid,
            'isclose' => 0,
        );

        $data = array(
            'from' => 'class',
            'column' => 'classlinkname,childnum,classid,isgotofirst,linkurl,classname,parentid',
            'where' => $where,
            'order' => 'listid desc',
        );
        $list = $this->_get_next_class($data);
        return $list;
    }

    /**
     * 获取导航菜单
     * @param int $configid 网站id
     * @return array 
     */
    function get_class_nav($configid = 1) {
        $where = array(
            'isshownav' => 1,
            'configid' => $configid,
            'parentid' => 0,
            'isclose' => 0,
        );
        $data = array(
            'from' => 'class',
            'column' => 'classlinkname,childnum,classid,isgotofirst,linkurl,classname,parentid',
            'where' => $where,
            'order' => 'listid desc',
        );
        $list = $this->_get_next_class($data);
        $this->CI->load->helper('array_func_helper');
        if(!empty($list)){
            $return = _findChildren($list,0);
        }
        return $return;
    }

    /**
     * 获取单个栏目
     * @param type $classid
     * @return type
     */
    function get_class($classid) {
        $data = array();
        if (is_int($classid)) {
            $data['classid'] = $classid;
        } else {
            $data['classlinkname'] = $classid;
        }
        return $this->m_public->getRowInfo('class', $data);
    }

    /**
     * 获取单个模版
     * @param type $classtemplateid
     * @return type
     */
    function get_classtemplate($classtemplateid) {
        $data = array('classtemplateid' => $classtemplateid);
        return $this->m_public->getRowInfo('classtemplate', $data);
    }

    /*
     * 递归查找子栏目
     */
    private function _get_next_class($data) {
        $list = $this->m_public->get_list($data);
        foreach ($list as $k => $v) {
            if ($v['childnum'] > 0) {
                $data['where']['parentid'] = $v['classid'];
                $list[$k]['child'] = $this->_get_next_class($data);
            } else {
                $list[$k]['child'] = array();
            }
        }
        return $list;
    }

}
